import { mapState, mapMutations } from 'vuex'
import { getQueryString } from '../../config/util'
export default {
	name: 'search',
	data() {
		return {
			isEx: true, //交换城市动画需要
		}
	},
	computed: {
		...mapState([
			'isBack', 'isInland', 'cabin', 'city', 'Date', 'adultCount','tokenAccess'
		]),
		//往返状态的读取和更改
		isBack_: {
			// getter
			get: function() {
				if(this.isBack) return true;
				else return false;
			},
			// setter
			set: function(newValue) {
				this.RECORD_ISBACK(newValue); //记录往返状态
			}
		},
		adultCount_: {
			get: function() {							
				return this.adultCount;
			},
			set: function(newValue) {
				if(newValue < 1) return false;
				if(newValue > 4) return false;
				else this.RECORD_ADULTCOUNT(newValue);			
			}
		},
		depDate_: {
			get: function() {
				return this.Date.depDate;
			},
			set: function(newValue) {
				this.RECORD_DATE({ type: 'dep', value: newValue });
			}
		},
		arrDate_: {
			get: function() {
				return this.Date.arrDate;
			},
			set: function(newValue) {
				this.RECORD_DATE({ type: 'arr', value: newValue });
			}
		}
	},
	methods: {
		...mapMutations([
			'RECORD_ISBACK', 'RECORD_CABIN', 'RECORD_CITY', 'RECORD_DATE', 'RECORD_ADULTCOUNT','RECORD_PASSENGERLIST',
			'RECORD_INSURANCE','RECORD_TOKEN','RECORD_TOKENACCESS','RECORD_LAKALAUSER'
		]),
		//出发城市与到达城市的交换
		exchange() {
			this.RECORD_CITY("ex")					
			this.isEx = !this.isEx;
		},
		//舱位的选择
		handleCabinChange(val) {
			//更改舱位状态
			if(val == "Y") this.RECORD_CABIN(true);
			else this.RECORD_CABIN(false);
			if(val) this.RECORD_CITY(val);
			else alert("城市数据有误");
		},

		//轮播图滚动触发的事件
		handleChange(index) {

		},
		order() {
			this.$router.push({ path: '/order' });
		},
		help() {
			this.$router.push({ path: '/help' });
		},
		search() {
			this.RECORD_PASSENGERLIST("clear");
			this.RECORD_INSURANCE({type:"clear"});
			this.$router.push({ path: '/flight' });
		},
		toCity(type) {
			this.RECORD_CITY(type);
			this.$router.push({ path: '/city' });
		},
		toDate(type) {
			this.RECORD_DATE(type);
			this.$router.push({ path: '/date' });
		},
		//获取token
		getToken(){	
			//参数的缓存
			var Sign,ResData,AccessChannel
			if(!this.tokenAccess.Sign){
				Sign = getQueryString('Sign');
				ResData = getQueryString('ResData');
				AccessChannel = getQueryString('AccessChannel');
				this.RECORD_TOKENACCESS({'Sign':Sign,'ResData':ResData,'AccessChannel':AccessChannel});
			}else{
				Sign = this.tokenAccess.Sign;
				ResData = this.tokenAccess.ResData;
				AccessChannel = this.tokenAccess.AccessChannel;
			}
			
			var url = '/user/logintest?temp=' + Math.random();//非拉卡拉入口
			if(Sign&&ResData&&AccessChannel){
				url = '/user/login?temp=' + Math.random();//拉卡拉入口
			}
			this.$axios.post(url, {
					"Sign":Sign,
	            	"ResData":ResData,
	            	"AccessChannel":AccessChannel
				})
				.then((res) => {
					if(res.data.code==200&&res.data.token){
						this.RECORD_TOKEN(res.data.token);
						this.RECORD_LAKALAUSER({name:res.data.name,mobile:res.data.mobile});
					}
				})	
		}
	},
	created() {
		if(!this.isInland){
			this.RECORD_ADULTCOUNT(1);
		}
		document.title = "航班搜索";
		this.getToken();//获取登录token
	}
}